package com.yc.sole.system.controller;

import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import com.yc.sole.system.biz.MenuBiz;
import com.yc.sole.system.request.MenuQueryReq;
import com.yc.sole.system.request.MenuReq;
import com.yc.sole.system.response.MenuResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 平台菜单管理
 *
 * @author yizuomin
 * @date 2023-03-26
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class MenuController {

    private final MenuBiz menuBiz;

    /**
     * 新增
     *
     * @param menuReq
     * @return RestResult
     */
    @PreAuth(value = "system:menu:save", description = "新增菜单")
    @PostMapping("/api/system/menu/saveMenu")
    public Result<Void> saveMenu(@RequestBody @Validated(SaveGroup.class) MenuReq menuReq) {
        menuBiz.saveMenu(menuReq);
        return Result.success();
    }

    /**
     * 通过Id修改
     *
     * @param menuReq
     * @return RestResult
     */
    @PreAuth(value = "system:menu:update", description = "修改菜单")
    @PostMapping("/api/system/menu/updateMenuById")
    public Result<Void> updateMenuById(@RequestBody @Validated(UpdateGroup.class) MenuReq menuReq) {
        menuBiz.updateMenuById(menuReq);
        return Result.success();
    }

    /**
     * 通过Id查询菜单
     *
     * @param menuId
     * @return RestResult
     */
    @PreAuth(value = "system:menu:select", description = "通过Id查询菜单")
    @PostMapping("/api/system/menu/selectMenuById/{menuId}")
    public Result<MenuResp> selectMenuById(@PathVariable Long menuId) {
        return Result.data(menuBiz.selectMenuById(menuId));
    }

    /**
     * 通过id删除
     *
     * @param baseDeleteReq
     * @return RestResult
     */
    @PreAuth(value = "system:menu:delete", description = "删除菜单")
    @PostMapping("/api/system/menu/deleteMenu")
    public Result<Void> deleteMenu(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseDeleteReq) {
        menuBiz.deleteMenu(baseDeleteReq.getIds());
        return Result.success();
    }

    /**
     * 菜单树
     *
     * @param req
     * @return Result
     */
    @PreAuth(value = "system:menu:select", description = "查询菜单树")
    @PostMapping("/api/system/menu/selectMenuTree")
    public Result<List<MenuResp>> selectMenuTree(@RequestBody MenuQueryReq req) {
        return Result.data(menuBiz.selectMenuTree(req));
    }
}
